package com.shangma.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangma.cn.commons.page.PageBean;
import com.shangma.cn.dto.base.impl.BrandDTO;
import com.shangma.cn.dto.base.impl.RoleDTO;
import com.shangma.cn.entity.base.impl.Brand;
import com.shangma.cn.entity.base.impl.Menu;
import com.shangma.cn.entity.base.impl.Role;
import com.shangma.cn.entity.base.impl.RoleMenu;
import com.shangma.cn.mapper.MenuMapper;
import com.shangma.cn.mapper.RoleMapper;
import com.shangma.cn.mapper.RoleMenuMapper;
import com.shangma.cn.query.impl.RoleQuery;
import com.shangma.cn.service.RoleService;
import com.shangma.cn.service.base.impl.BaseServiceImpl;
import com.shangma.cn.transfer.base.impl.RoleTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 开发人员: 辉哥真球帅
 * 联系方式: zhengzhoudaxuevip@163.com
 * 创建时间: 2021/10/11  16:20
 * 文件含义:
 */
@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {


    public final RoleMapper roleMapper;
    public final RoleTransfer roleTransfer;

    public final MenuMapper menuMapper;

    public final RoleMenuMapper roleMenuMapper;

    @Override
    public PageBean<RoleDTO> searchPage(RoleQuery roleQuery) {
        PageHelper.startPage(roleQuery.getCurrentPage(), roleQuery.getPageSize());
        List<Role> roles = null;
        if (roleQuery.isQuery()) {
            LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
            if (!StringUtils.isEmpty(roleQuery.getRoleName())) {
                lambda.like(Role::getRoleName, roleQuery.getRoleName());
            }
            if (roleQuery.getStartTime() != null && roleQuery.getEndTime() != null) {
                lambda.between(Role::getCreateTime, roleQuery.getStartTime(), roleQuery.getEndTime());
            }

            roles = roleMapper.selectList(lambda);
        } else {
            roles = roleMapper.selectList(null);
        }
        PageInfo<Role> pageInfo = new PageInfo<>(roles);
        long total = pageInfo.getTotal();
        PageBean<RoleDTO> pageBean = new PageBean<>(total, roleTransfer.toDTOS(roles));
        return pageBean;

    }

    @Override
    public int setRoleMenu(Long roleId, List<Long> menuIds) {
        roleMenuMapper.delete(new UpdateWrapper<RoleMenu>().lambda().eq(RoleMenu::getRoleId, roleId));
        menuIds.forEach(menuId -> roleMenuMapper.insert(new RoleMenu(roleId, menuId)));
        return 1;
    }

    @Override
    public List<Long> getRoleMenusById(Long id) {
        List<RoleMenu> roleMenus = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>().lambda().eq(RoleMenu::getRoleId, id));
        List<Long> collect = roleMenus.stream().map(roleMenu -> roleMenu.getMenuId()).collect(Collectors.toList());
        //所有的权限都在这里了
        //  1:遍历所有的权限
        // 2: 遍历的时候 再从这个里面找没有孩子的保留 有孩子的删除
        if (!CollectionUtils.isEmpty(collect)) {
            List<Menu> menus = menuMapper.selectBatchIds(collect);
            collect.clear();
            menus.forEach(menu -> {
                if (!menus.stream().anyMatch(menu1 -> menu1.getParentId().equals(menu.getId()))) {
                    collect.add(menu.getId());
                }
            });
        }
        return collect;
    }

    //用户的所有的权限 没有重复
    @Override
    public List<Menu> getMenusByRoleId(List<Long> roleIds) {
        List<Long> menuIds = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>().lambda().in(RoleMenu::getRoleId, roleIds)).stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        List<Menu> menus = menuMapper.selectBatchIds(menuIds);
        return menus;
    }
}
